<pre><?php

/**
 * This test file for simplesql.class.php exemplifies
 * how easy it is to manipluate a database. This test
 * will list various methods of how to use functions
 * and methods. 
 *
 * For more examples and extended documentation, visit
 * my directory at PHPclasses.org 
 * http://phpclasses.mirrors.iplexx.at/browse/author/166377.html
 * or email me at webmaster@protonage.net
 */
 
require_once('simplesql.class.php5');

/**
 * Construction
 *
 * If your database connection variables are already
 * hard coded into the class file, then you may just simply
 * create a new class without any arguments. But for this
 * example I will include the basic connection arguments
 * to get you started on how the class functions.
 */
 
$ini = parse_ini_file("web.ini");
while(list($key,$value) = each($ini)){
	if($key == 'hostName'){
		$hostName = $value;
	}
	else if($key == 'userName'){
		$userName = $value;
	}
	else if($key == 'password'){
		$password = $value;
	}
	else if($key == 'dbName'){
		$dbName = $value;
	}
}
$db = new simplesql($hostName,$userName,$password,$dbName);

/**
 * Setup
 *
 * This is a query to setup a table that this
 * file will use.
 *
 * If for some reason the query throws an exception
 * then this try catch will handel it.
 */
try {
    $query = "CREATE TABLE `simplesql_cars` (
        `modelid` int(10) NOT NULL auto_increment,
        `modelname` varchar(255) NOT NULL default '',
        `active` tinyint(1) NOT NULL default '0',
        `location` varchar(255) NOT NULL default '',
        `content` varchar(255) NOT NULL default '',
        PRIMARY KEY  (`modelid`)
    ) TYPE=MyISAM AUTO_INCREMENT=0 ;";
    $db->query(trim($query));
    $db->insert(
        'modelname, active, location, content',
        "'Thunderbird 4100', 0, 'Williamsburg, VA', 'http://www.google.com/'", 'simplesql_cars'
    );
    $db->insert(
        'modelname, active, location, content',
        "'Dodge Viper', 1, 'Denver, CO', 'http://www.protonage.net'"
    );
    $db->insert(
        'modelname, active, location, content',
        "'Range Rover', 1, 'Reston, VA', 'http://www.google.com/'"
    );
    $db->insert(
        'modelname, active, location, content',
        "'Ford F150', 1, 'NewYork, NY', 'http://www.google.com/'"
    );
    $db->insert(
        'modelname, active, location, content',
        "'Toyota Corral', 0, 'Richmond, VA', 'http://www.google.com/'"
    );
    $db->insert(
        'modelname, active, location, content',
        "'BMW Z3', 1, 'Pitsburge, PA', 'http://www.dictionary.com/'"
    );
    
} catch (Exception $e) {
    trigger_error($e->getmessage(), E_USER_ERROR);
    exit;
}
echo 'Search Example: 
';
/**
 * Search
 *
 * This example searches the field location and returns
 * every row that contains the keyword.
 *
 * First argument is the keyword to be found
 * Second argument is the field that will be searched
 * Third argument is the table that will be used
 * +  Note, this argument is optional in ALL methods and functions
 * +  because everytime it is called in a function, the table name
 * +  is stored in a variable inside the class. Later on in this test
 * +  file you will see that I will no longer use the table argument 
 * Fourth argument defines how you want the output to be formed, in 
 * +  this case, I wanted the result to be an object array.
 */ 
print_r($db->fetch_search('VA', 'location', 'simplesql_cars', FETCH_OBJECT));

echo 'Fetch Column Example: 
';
/**
 * Fetch Column
 *
 * This example grabs columns of data out of the database
 * 
 * First argument is a list of fields and one field (active)
 * +  which i gave a value of 1. This means the result will only
 * +  pick up the listed fields and where active = 1. Easy? Indeed!
 * Second argument is the table but I omitted it because I already
 * +  defined the table I want to keep querying in the last function.
 * Third argument (always optional and is saved in the class) is the
 * +  fetch type. This time I fetched the results as an assoiciate array.
 */
print_r($db->fetch_col('modelname, location, active = 1', null, FETCH_ASSOC));

echo 'Last Query Example: 
';
/**
 * Last Query
 *
 * This function will return the last query executed.
 *
 * Wanna see how that last function went threw?
 */
print_r($db->get_lastquery());

echo '

Fetch Row Example: 
';
/**
 * Fetch Row
 *
 * This example will fetch a row from a database
 *
 * First argument simply supply the field and it's value
 * +  Note: if the value is a string simply encapsulate it with
 * +  some type of quote ['`"] 
 */
print_r($db->fetch_row('modelid = 3'));

echo 'Fetch Example: 
';
/**
 * Fetch
 *
 * This example will return the result of a query
 *
 * First argument is the query you want to send
 * Second argument is how you want the result to be returned
 */
print_r($db->fetch_query('SELECT * FROM `simplesql_cars` WHERE modelid < 6 AND modelid > 2', FETCH_NUM));

echo 'Get Result Example: 
';
/**
 * Get Result
 * 
 * Want to grab the result from the last SELECT query?
 * Need the number of rows from last query?
 */
print_r($db->get_result());
echo '
get_rows Example: 
';
var_dump($db->get_rows());

echo 'Fetch Column Example: 
';
/**
 * Fetch Column
 *
 * This example shows how to use the extra optional parameters in the function
 * 
 * First argument, again are the fields. This time I wanted to 
 * +  fetch where content = "http://www.google.com/"
 * Second argument Table to use (omitted) because it was cached
 * Third argument, How I want the result returned. This time I want an associate array.
 * Fourth argument, Order by clause, I ordered by modelid field DESCENDING results
 * Fith argument, Limit clause, limit 1, 2 will only return the second and third result
 * +  from the query.
 */
print_r($db->fetch_col('modelname, content, location, content = "http://www.google.com/"', null, FETCH_ASSOC, 'modelid DESC', '1, 2'));

/**
 * Insert
 *
 * This example shows you how to insert data in a table
 * These arguments CAN be an array of data! they just have to
 * match in length.
 *
 * First argument is the list of fields I want to insert data into
 * Second argument is the values given to thoes fields
 */
$db->insert('modelname, location, content', '"Mazda RX8", "Googleville, DC", "http://www.dotgeek.org"');

echo 'Show last query Example: 
';
// Show last query
echo $db->get_lastquery();

/**
 * Clone of above example except this uses arrays
 */
$fields = array('modelname', 'location', 'content');
$values = array("Mazda RX8", "Googleville, DC", "http://www.dotgeek.org");
$db->insert($fields, $values);

/**
 * Update
 *
 * This example will update content in a database
 * Updates the 5th id and turns active to 1
 * Note this function also accepts arrays for arguments
 *
 * First argument is the field that will change
 * Second argument is it's new value
 * Thrid is the where clause, if any.
 */
$db->update('active', '1', 'modelid = 5');

/**
 * Delete
 * 
 * Delete a row or truncate a table
 * This is a dangerouse function so I added in 
 * a paramater called $pass, What this function does is
 * if the where fields argument is empty and $pass is omitted
 * then the function will halt and return a Warning.
 * if the $pass boolean is set to true, then this warning
 * will be omitted and the query will be executed.
 * This ONLY happens when the table is about to be truncated
 *
 * First argument is the values of the rows you want to delete
 * Other arguments like $pass (explained above) and limit can be used
 * Limit is useful if you want to delete only x number of rows.
 */
$db->delete("location = 'Richmond, VA'"); 

/**
 * Misc Functions
 *
 * get_log will return the log array or if you supply it a key
 * +  from the array, it will return that key as an array only
 */
// Will return the mysql log array
// print_r($db->get_log('mysql'));
// Will return the entire log array
// print_r($db->get_log());

/**
 * field_exists will return true if a field exists in a table
 * +  and false if not, This function is used by many other functions
 * +  within the class, but it has public access
 */
echo 'field_exists(\'active\') Example: 
';
var_dump($db->field_exists('active'));
echo 'field_exists(\'i_dont_exists\') Example: 
';
var_dump($db->field_exists('i_dont_exists'));

/**
 * get_error() will return the last error if argument 
 * +  is false or not given otherwise if true, it returns array
 */
 
/**
 * selectdb($dbname) if you need to switch DB, simply call
 * +  this function with the name of the DB in the argument
 */
 
/**
 * Setting Parameters
 *
 * You can set common booleans (usually for debugging) real time
 * list of them are: 
 * +      'tracklog' (build track log array)
 * +      'showlog' (show log at destruction of class)
 * +      'logtofile' (log track log to file)
 * +      'logfulltofile' (log full log array to raw serialize file)
 *
 * Note: You are not accessing these variables directly
 * What happens is these variables are stored inside a protected
 * array named common, when you get/set these variables, PHP5
 * sees that they do not exists and then sends them to __set/__get
 * functions, it is there where they are picked up and shoved
 * back or from the array.
 */   
// This will print the entire log array to the screen when the class destructs itself
// It's default value is false, but this an example of how to change these common booleans
// and an example of what when on in this test/example file.
$db->showlog = true;

/**
 * That's not all of what this class file has to offer
 * Play around with it and you really see the power you can
 * get from out of this class.
 *
 * The debugging is especially amazing, You can grab query intensity 
 * levels from the mysql log array, the class also logs every single move
 * it makes to a serialized entry to a raw log file!
 *
 * Soon I will add a mysqli layer for this class, note the one final class
 * and also I will make a debug layer complete with pie graphs and
 * optimization solutions!
 */

?></pre>